package hangman.Ana.Gilles;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.ImageButton;
import java.util.Random;
import android.app.AlertDialog;
import android.content.DialogInterface;


//Pop up menu that will ask to select a difficulty and start game according to difficulty
public class Game extends Activity implements OnClickListener{
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.game);
      new AlertDialog.Builder(this)
		.setTitle(R.string.new_game_title)
		.setItems(R.array.difficulty,
		new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int a) {
				getWord(a);
			}
		})
		.show();   
   }

   //setup for click listenners
   public void setClickListenners(){
	   View aButton = findViewById(R.id.buttonA);
	   aButton.setOnClickListener(this);
	   View bButton = findViewById(R.id.buttonB);
	   bButton.setOnClickListener(this);
	   View cButton = findViewById(R.id.buttonC);
	   cButton.setOnClickListener(this);
	   View dButton = findViewById(R.id.buttonD);
	   dButton.setOnClickListener(this);
	   View eButton = findViewById(R.id.buttonE);
       eButton.setOnClickListener(this);
       View fButton = findViewById(R.id.buttonF);
	   fButton.setOnClickListener(this);
	   View gButton = findViewById(R.id.buttonG);
	   gButton.setOnClickListener(this);
       View hButton = findViewById(R.id.buttonH);
       hButton.setOnClickListener(this);
       View iButton = findViewById(R.id.buttonI);
	   iButton.setOnClickListener(this);
	   View jButton = findViewById(R.id.buttonJ);
	   jButton.setOnClickListener(this);
	   View kButton = findViewById(R.id.buttonK);
	   kButton.setOnClickListener(this);
	   View lButton = findViewById(R.id.buttonL);
	   lButton.setOnClickListener(this);
	   View mButton = findViewById(R.id.buttonM);
	   mButton.setOnClickListener(this);
	   View nButton = findViewById(R.id.buttonN);
       nButton.setOnClickListener(this);
       View oButton = findViewById(R.id.buttonO);
	   oButton.setOnClickListener(this);
	   View pButton = findViewById(R.id.buttonP);
	   pButton.setOnClickListener(this);
	   View qButton = findViewById(R.id.buttonQ);
	   qButton.setOnClickListener(this);
	   View rButton = findViewById(R.id.buttonR);
	   rButton.setOnClickListener(this);
	   View sButton = findViewById(R.id.buttonS);
	   sButton.setOnClickListener(this);
	   View tButton = findViewById(R.id.buttonT);
	   tButton.setOnClickListener(this);
	   View uButton = findViewById(R.id.buttonU);
	   uButton.setOnClickListener(this);
	   View vButton = findViewById(R.id.buttonV);
	   vButton.setOnClickListener(this);
	   View wButton = findViewById(R.id.buttonW);
	   wButton.setOnClickListener(this);
	   View xButton = findViewById(R.id.buttonX);
	   xButton.setOnClickListener(this);
	   View yButton = findViewById(R.id.buttonY);
	   yButton.setOnClickListener(this);
	   View zButton = findViewById(R.id.buttonZ);
	   zButton.setOnClickListener(this);
	   View hintButton = findViewById(R.id.hintbutton);
	   hintButton.setOnClickListener(this); 
   }
   
   //char array that will contain hidden word
   char word[]={' ',' ',' ',' ',' ',' ',' ',' '};
   int chances=6; //amount of wrong answers allowed
   int line;	//this variable will contain the random number of the line from which the word will be selected
   int win;	//will decrement and will declare win at 0
   String easy[]={"LEVEL","WATCH","CROCS"}; //array of easy words 5 letters
   String easyHint[]={"This should be a hint about the word... " +
   								"but for testing purposes, the word is: LEVEL",
   						"This object gives you The time",
   						"Enormous reptile"}; //hints for these words
   
   String medium[]={"LEVELS","CRACKS","CLOCKS"};	//array of medium difficulty words
   String mediumHint[]={"This should be a hint about the word... " +
						"but for testing purposes, the word is: LEVELS",
						"CRACKS","CLOCKS"}; //hints
   
   String hard[]={"LEVELED","WATCHED","MARINES"}; //array of hard words
   String hardHint[]={"This should be a hint about the word..." +
   						"but for testing purposes, the word is: LEVELED",
   						"WATCHED","MARINES"}; //hints
   
   String temp[]={" "," "," "};//temporary array that will hold the values from the array of the selected difficulty
   String tempHint[]={" "," "," "};//hints
   Random rand;//will hold the random number
   
   //this method will decide which array will be copied to the temp array
   public void getWord(int a){
	   setClickListenners(); 
	   switch(a){
	   		case 0:	//if easy is selected
	   			System.arraycopy(easy, 0, temp, 0, easy.length); //copy the easy array to temp
	   			System.arraycopy(easyHint, 0, tempHint, 0, easyHint.length); //copy hints
	   			win = 5; 	//5 Letters to win
	   			break;
	   		case 1:	//if medium is selected
	   			System.arraycopy(medium, 0, temp, 0, medium.length);//copy the medium array to temp
	   			System.arraycopy(mediumHint, 0, tempHint, 0, mediumHint.length);//copy hints
	   			findViewById(R.id.letter6).setVisibility(1); //enables the 6th letter in the view
	   			win = 6;	//6 letters to win
	   			break;
	   		case 2:	//if hard is selected
	   			System.arraycopy(hard, 0, temp, 0, hard.length);//copy the hard array to temp
	   			System.arraycopy(hardHint, 0, tempHint, 0, hardHint.length);//copy hints
	   			findViewById(R.id.letter6).setVisibility(1); //enables the 6th letter in the view
	   			findViewById(R.id.letter7).setVisibility(1); //enables the 7th letter in the view
	   			win = 7;	//7letters to win
	   			break;
	   }
	   read();	  //calls read
   }
   
   public void read() {
	   //get a random number between 0 and length oh temp
	   line = 0 + (int)(Math.random() * ((temp.length - 1) + 1));
	   String str = temp[line]; //assigns the string to str
		   word[1] =str.charAt(0);//copies the 1st char in str to the char array word
		   word[2] =str.charAt(1);
		   word[3] =str.charAt(2);
		   word[4] =str.charAt(3);
		   word[5] =str.charAt(4);
		   if(win == 6){
			   word[6] =str.charAt(5);
		   }
		   if(win == 7){
			   word[6] =str.charAt(5);
			   word[7] =str.charAt(6);
		   }
   }
  
   //checking if the entered letter is good
   public void checkLetter(char letter){
	  boolean good=false;
	  for(int x=1;x<word.length;x++){ //while there are unchecked letters in the array
		  if(letter==word[x]){ //if letter is found
			  switch(x){	//switch to the right letter spot
			  		case 1:
			  			TextView text = (TextView)findViewById(R.id.letter1);
			  			text.setText(" "+ word[x]);
			  			break;
		  			case 2:
		  				TextView text2 = (TextView)findViewById(R.id.letter2);
		  				text2.setText(" "+ word[x]);
		  				break;
		  			case 3:
		  				TextView text3 = (TextView)findViewById(R.id.letter3);
		  				text3.setText(" "+ word[x]);
		  				break;
		  			case 4:
		  				TextView text4 = (TextView)findViewById(R.id.letter4);
		  				text4.setText(" "+ word[x]);
		  				break;
		  			case 5:
		  				TextView text5 = (TextView)findViewById(R.id.letter5);
		  				text5.setText(" "+ word[x]);
		  				break;
		  			case 6:
		  				TextView text6 = (TextView)findViewById(R.id.letter6);
		  				text6.setText(" "+ word[x]);
		  				break;
		  			case 7:
		  				TextView text7 = (TextView)findViewById(R.id.letter7);
		  				text7.setText(" "+ word[x]);
		  				break;
			  }
			  good = true;	//it was a good letter
			  win=win-1;	//one step closer to a win
		  }
	  }
	  
	  if(good==false){ //if wrong letter
		  chances = chances-1; //lose a chance
		  TextView text = (TextView)findViewById(R.id.chances);
		  text.setText("chances: " + chances); //change text to one less chance
		  ImageView image = (ImageView)findViewById(R.id.imageView1);	//assigning view to image
		  switch(chances){
	  		case 0:	//if you have no chances left
	  			image.setImageResource(R.drawable.hang0); //change image
	  			text.setText("you lose");	//set text
	  			Intent j = new Intent (this, Lose.class);	//assign to j the class (dialog theme) that show that you lost
	    		startActivity(j); //start j
	  			break;
	  		case 1:	  			
	  			image.setImageResource(R.drawable.hang1); //change image
	  			break;
	  		case 2:	  			
	  			image.setImageResource(R.drawable.hang2); //change image
	  			break;
	  		case 3:	  			
	  			image.setImageResource(R.drawable.hang3); //change image
	  			break;
	  		case 4:	  			
	  			image.setImageResource(R.drawable.hang4); //change image
	  			break;
	  		case 5:	  			
	  			image.setImageResource(R.drawable.hang5); //change image
	  			break;
	  		case 6:	  			
	  			image.setImageResource(R.drawable.hang6); //change image
	  			break;
			
	 	}
	  }
	  if(win==0){	//if player won
		  TextView text2 = (TextView)findViewById(R.id.textView3);
		  text2.setText("You won!");	//set text to you won
		  Intent j = new Intent (this, Win.class);//assign to j the class (dialog theme) that show that you won
		  startActivity(j);	//start j
	  }
   }

   //returns the hint for the hidden word as a String
   public String getHint(){	
	   return tempHint[line];
   }
   
   @Override
   public void onClick(View v) {
	   if(v.getId() == R.id.hintbutton){ //if user asks for the hint, display it in a dialog box
		   AlertDialog.Builder hintDialog = new AlertDialog.Builder(this);
		   hintDialog.setTitle("Hint").setMessage(getHint());
		   hintDialog.show();
	   }
	   else{//other buttons that correspond to a letter.
		   	ImageButton image = (ImageButton)findViewById(v.getId());
	   		switch(v.getId()){
	   			case R.id.buttonA:
	   				image.setImageResource(R.drawable.a2);	//change the image to a crossed out one
	   				checkLetter('A');
	   				break;
	   			case R.id.buttonB:
	   				image.setImageResource(R.drawable.b2);	//change the image to a crossed out one
	   				checkLetter('B');
	   				break;
	   			case R.id.buttonC:
	   				image.setImageResource(R.drawable.c2);	//change the image to a crossed out one
	   				checkLetter('C');
	   				break;
	   			case R.id.buttonD:
	   				image.setImageResource(R.drawable.d2);	//change the image to a crossed out one
	   				checkLetter('D');
	   				break;
	   			case R.id.buttonE:
	   				image.setImageResource(R.drawable.e2);	//change the image to a crossed out one
	   				checkLetter('E');
	   				break;
	   			case R.id.buttonF:
	   				image.setImageResource(R.drawable.f2);	//change the image to a crossed out one
	   				checkLetter('F');
	   				break;
	   			case R.id.buttonG:
	   				image.setImageResource(R.drawable.g2);	//change the image to a crossed out one
	   				checkLetter('G');
	   				break;
	   			case R.id.buttonH:
	   				image.setImageResource(R.drawable.h2);	//change the image to a crossed out one
	   				checkLetter('H');
	   				break;
	   			case R.id.buttonI:
	   				image.setImageResource(R.drawable.i2);	//change the image to a crossed out one
	   				checkLetter('I');
	   				break;
	   			case R.id.buttonJ:
	   				image.setImageResource(R.drawable.j2);	//change the image to a crossed out one
	   				checkLetter('J');
	   				break;
	   			case R.id.buttonK:
	   				image.setImageResource(R.drawable.k2);	//change the image to a crossed out one
	   				checkLetter('K');
	   				break;
	   			case R.id.buttonL:
	   				image.setImageResource(R.drawable.l2);	//change the image to a crossed out one
	   				checkLetter('L');
	   				break;
	   			case R.id.buttonM:
	   				image.setImageResource(R.drawable.m2);	//change the image to a crossed out one
	   				checkLetter('M');
	   				break;
	   			case R.id.buttonN:
	   				image.setImageResource(R.drawable.n2);	//change the image to a crossed out one
	   				checkLetter('N');
	   				break;
	   			case R.id.buttonO:
	   				image.setImageResource(R.drawable.o2);	//change the image to a crossed out one
	   				checkLetter('O');
	   				break;
	   			case R.id.buttonP:
	   				image.setImageResource(R.drawable.p2);	//change the image to a crossed out one
	   				checkLetter('P');
	   				break;
	   			case R.id.buttonQ:
	   				image.setImageResource(R.drawable.q2);	//change the image to a crossed out one
	   				checkLetter('Q');
	   				break;
	   			case R.id.buttonR:
	   				image.setImageResource(R.drawable.r2);	//change the image to a crossed out one
	   				checkLetter('R');
	   				break;
	   			case R.id.buttonS:
	   				image.setImageResource(R.drawable.s2);	//change the image to a crossed out one
	   				checkLetter('S');
	   				break;
	   			case R.id.buttonT:
	   				image.setImageResource(R.drawable.t2);	//change the image to a crossed out one
	   				checkLetter('T');
	   				break;
	   			case R.id.buttonU:
	   				image.setImageResource(R.drawable.u2);	//change the image to a crossed out one
	   				checkLetter('U');
	   				break;
	   			case R.id.buttonV:
	   				image.setImageResource(R.drawable.v2);	//change the image to a crossed out one
	   				checkLetter('V');
	   				break;
	   			case R.id.buttonW:
	   				image.setImageResource(R.drawable.w2);	//change the image to a crossed out one
	   				checkLetter('W');
	   				break;
	   			case R.id.buttonX:
	   				image.setImageResource(R.drawable.x2);	//change the image to a crossed out one
	   				checkLetter('X');
	   				break;
	   			case R.id.buttonY:
	   				image.setImageResource(R.drawable.y2);	//change the image to a crossed out one
	   				checkLetter('Y');
	   				break;
	   			case R.id.buttonZ:
	   				image.setImageResource(R.drawable.z2);	//change the image to a crossed out one
	   				checkLetter('Z');	
	   				break;
	   		}
	   		v.setClickable(false);
	   	}
   }
   
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event){
	   switch(keyCode){	//if user uses the keyboard instead of the views on the screen... it will call the corresponding views
			case KeyEvent.KEYCODE_A:
				onClick(findViewById(R.id.buttonA));
   				break;
			case KeyEvent.KEYCODE_B:
				onClick(findViewById(R.id.buttonB));
   				break;
			case KeyEvent.KEYCODE_C:
				onClick(findViewById(R.id.buttonC));
   				break;
			case KeyEvent.KEYCODE_D:
				onClick(findViewById(R.id.buttonD));
   				break;
			case KeyEvent.KEYCODE_E:
				onClick(findViewById(R.id.buttonE));
   				break;
			case KeyEvent.KEYCODE_F:
				onClick(findViewById(R.id.buttonF));
   				break;
			case KeyEvent.KEYCODE_G:
				onClick(findViewById(R.id.buttonG));
   				break;
			case KeyEvent.KEYCODE_H:
				onClick(findViewById(R.id.buttonH));
   				break;
			case KeyEvent.KEYCODE_I:
				onClick(findViewById(R.id.buttonI));
   				break;
			case KeyEvent.KEYCODE_J:
				onClick(findViewById(R.id.buttonJ));
   				break;
			case KeyEvent.KEYCODE_K:
				onClick(findViewById(R.id.buttonK));
   				break;
			case KeyEvent.KEYCODE_L:
				onClick(findViewById(R.id.buttonL));
   				break;
			case KeyEvent.KEYCODE_M:
				onClick(findViewById(R.id.buttonM));
   				break;
			case KeyEvent.KEYCODE_N:
				onClick(findViewById(R.id.buttonN));
   				break;
			case KeyEvent.KEYCODE_O:
				onClick(findViewById(R.id.buttonO));
   				break;
			case KeyEvent.KEYCODE_P:
				onClick(findViewById(R.id.buttonP));
   				break;
			case KeyEvent.KEYCODE_Q:
				onClick(findViewById(R.id.buttonQ));
   				break;
			case KeyEvent.KEYCODE_R:
				onClick(findViewById(R.id.buttonR));
   				break;
			case KeyEvent.KEYCODE_S:
				onClick(findViewById(R.id.buttonS));
   				break;
			case KeyEvent.KEYCODE_T:
				onClick(findViewById(R.id.buttonT));
   				break;
			case KeyEvent.KEYCODE_U:
				onClick(findViewById(R.id.buttonU));
   				break;
			case KeyEvent.KEYCODE_V:
				onClick(findViewById(R.id.buttonV));
   				break;
			case KeyEvent.KEYCODE_W:
				onClick(findViewById(R.id.buttonW));
   				break;
			case KeyEvent.KEYCODE_X:
				onClick(findViewById(R.id.buttonX));
   				break;
			case KeyEvent.KEYCODE_Y:
				onClick(findViewById(R.id.buttonY));
   				break;
			case KeyEvent.KEYCODE_Z:
				onClick(findViewById(R.id.buttonZ));
   				break;
			case KeyEvent.KEYCODE_BACK:	//if user ever clicks on back, open main menu
				Intent j=new Intent (this, HangManActivity.class);
				startActivity(j);
				break;
	   }
	   return true;
   }
}

